﻿using System;
using System.Collections;
using System.Collections.Generic;
using offline2048;
using DG.Tweening;
using GTA;
using UnityEngine;
using UnityEngine.UI;
using MotionFramework.Audio;
using MotionFramework.Event;
using MotionFramework.Window;

namespace offline2048
{
    public class UIMain : MonoBehaviour
    {
        private EventGroup EventGrouper = new EventGroup();
        //购买失败相关====================================================================================
        private GameObject m_PurchaseFailed;
        private Button m_BtnPurchaseFailedClose;

        //自然量用户的shop界面代码==========================================================================
        //private GameObject m_MainShopTips;
        private Button m_BtnCloseMainShopTips;

        private Text m_TxtMainShopDoller;

        private const int MAIN_SHOP_DOLLER_ITEM_COUNT = 8;
        
        #region 框架

        void Awake()
        {
            OnCreate();
        }

        public void OnCreate()
        {
            //自然量用户的shop界面代码==========================================================================
            for (int i = 1; i <= MAIN_SHOP_DOLLER_ITEM_COUNT; i++)
            {
                UIMainShopItem m_MainShopTips = GetUIElement("Group/ScrollView/Viewport/Content/TopPic_" + i).GetComponent<UIMainShopItem>();
                CfgShop _table = ConfigManager.Instance.GetShopData(i.ToString());// CfgShop.GetConfigTable(i);
                m_MainShopTips.InitData(_table);
            }
            m_BtnCloseMainShopTips = GetUIComponent<Button>("Group/TitleGroup/Button");
            
            m_TxtMainShopDoller = GetUIComponent<Text>("Group/TopBg/Text_1");
            
            m_TxtMainShopDoller.text = $"{100}";
            
            m_BtnCloseMainShopTips.onClick.AddListener(OnClickCloseMainShopTipsBtn);
            
            //购买失败相关====================================================================================
            m_PurchaseFailed = GetUIElement("Purchase_failed").gameObject;
            m_BtnPurchaseFailedClose = GetUIComponent<Button>("Purchase_failed/Group/Button");
            
            m_BtnPurchaseFailedClose.onClick.AddListener(OnClickPurchaseFailedCloseBtn);
            
            EventGrouper.AddListener<UIMainEvent.MainShopIapFinish>(HandleEvent);
            EventGrouper.AddListener<UIMainEvent.MainShopIapFailed>(HandleEvent);
            
        }
        
        private GameObject GetUIElement(string path)
        {
            return this.transform.Find(path).gameObject;
        }
        
        private T GetUIComponent<T>(string path)
        {
            return this.transform.Find(path).gameObject.GetComponent<T>();
        }
        
        #endregion
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void HandleEvent(IEventMessage msg)
        {
            if (msg is UIMainEvent.MainShopIapFinish)
            {
                UIMainEvent.MainShopIapFinish _eventData = msg as UIMainEvent.MainShopIapFinish;
                GameManager.instance.CurrentData.CoinNumber += _eventData.m_ShopTable.GoodsNum / 100;
            }
            else if (msg is UIMainEvent.MainShopIapFailed)
            {
                m_PurchaseFailed.SetActive(true);
            }

        }
        
        private WaitForSeconds m_Wait_0_Point_1_Seconds = new WaitForSeconds(0.02f); 
        
        private void OnClickCloseMainShopTipsBtn()
        {
            MainManager.Instance.HideOganicShop();
        }
        
        private void OnClickPurchaseFailedCloseBtn()
        {

            m_PurchaseFailed.SetActive(false);
        }
        
    }
}
